package net.xju3_4.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.Result;
import net.xju3_4.service.AdminChangePasswordService;
import net.xju3_4.service.impl.AdminChangePasswordImpl;

import java.io.UnsupportedEncodingException;

@RequestMapping("/api/admin/changePassword")
public class AdminChangPasswordController {
    private AdminChangePasswordService adminChangePassword=new AdminChangePasswordImpl();
    @RequestMapping("/change.do")
    public void change(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String idStr = req.getParameter("id");
        Integer id = idStr == null || idStr.isEmpty() ? null : Integer.parseInt(idStr);
        String oldPassword = req.getParameter("oldPassword");
        String newPassword = req.getParameter("newPassword");
        if(adminChangePassword.changePassword(id,oldPassword,newPassword)>0)
            JSONUtil.printByJSON(resp,Result.success("修改密码成功"));
        else
            JSONUtil.printByJSON(resp, Result.fail("修改密码失败"));

    }
}
